﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Himp.Platform.Migrations
{
    /// <inheritdoc />
    public partial class add_table_AppBizflow : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "WorkflowModelAssocNode");

            migrationBuilder.DropTable(
                name: "WorkflowModelAssocType");

            migrationBuilder.DropTable(
                name: "WorkflowNode");

            migrationBuilder.DropTable(
                name: "WorkflowType");

            migrationBuilder.CreateTable(
                name: "MainAppBizflow",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    BizflowCode = table.Column<string>(type: "text", nullable: false),
                    BizflowName = table.Column<string>(type: "text", nullable: false),
                    Descr = table.Column<string>(type: "text", nullable: true),
                    AppTopicId = table.Column<Guid>(type: "uuid", nullable: false),
                    BizflowProvider = table.Column<int>(type: "integer", nullable: false),
                    BizflowProviderKey = table.Column<string>(type: "text", nullable: true),
                    WorkflowKey = table.Column<string>(type: "text", nullable: false),
                    WorkflowName = table.Column<string>(type: "text", nullable: false),
                    ValiFlag = table.Column<bool>(type: "boolean", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainAppBizflow", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MainAppBizflow_MainAppTopic_AppTopicId",
                        column: x => x.AppTopicId,
                        principalTable: "MainAppTopic",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "MainAppBiznode",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    NodeKey = table.Column<string>(type: "text", nullable: false),
                    NodeName = table.Column<string>(type: "text", nullable: false),
                    GrantRoles = table.Column<string[]>(type: "text[]", nullable: false),
                    GrantUsers = table.Column<string[]>(type: "text[]", nullable: false),
                    TemplateType = table.Column<int>(type: "integer", nullable: false),
                    TemplateKey = table.Column<string>(type: "text", nullable: false),
                    CanAuditFlag = table.Column<bool>(type: "boolean", nullable: false),
                    CanEditFlag = table.Column<bool>(type: "boolean", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MainAppBiznode", x => x.Id);
                });

            migrationBuilder.CreateIndex(
                name: "IX_MainAppBizflow_AppTopicId",
                table: "MainAppBizflow",
                column: "AppTopicId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "MainAppBizflow");

            migrationBuilder.DropTable(
                name: "MainAppBiznode");

            migrationBuilder.CreateTable(
                name: "WorkflowType",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    ParentId = table.Column<Guid>(type: "uuid", nullable: true),
                    DefaultFlag = table.Column<bool>(type: "boolean", nullable: false),
                    ExtraProperties = table.Column<string>(type: "text", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    Srtno = table.Column<int>(type: "integer", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    WorkflowTypeCodg = table.Column<string>(type: "text", nullable: false),
                    WorkflowTypeName = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowType", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WorkflowType_WorkflowType_ParentId",
                        column: x => x.ParentId,
                        principalTable: "WorkflowType",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "WorkflowModelAssocType",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    WorkflowTypeId = table.Column<Guid>(type: "uuid", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    WorkflowModeId = table.Column<string>(type: "text", nullable: false),
                    WorkflowModelName = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowModelAssocType", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WorkflowModelAssocType_WorkflowType_WorkflowTypeId",
                        column: x => x.WorkflowTypeId,
                        principalTable: "WorkflowType",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "WorkflowNode",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    WorkflowTypeId = table.Column<Guid>(type: "uuid", nullable: false),
                    ExtraProperties = table.Column<string>(type: "text", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    Schema = table.Column<string>(type: "text", nullable: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    WorkflowIndex = table.Column<int>(type: "integer", nullable: false),
                    WorkflowModelId = table.Column<string>(type: "text", nullable: false),
                    WorkflowNodeName = table.Column<string>(type: "text", nullable: false),
                    WorkflowNodeType = table.Column<int>(type: "integer", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowNode", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WorkflowNode_WorkflowType_WorkflowTypeId",
                        column: x => x.WorkflowTypeId,
                        principalTable: "WorkflowType",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "WorkflowModelAssocNode",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    WorkflowNodeId = table.Column<Guid>(type: "uuid", nullable: false),
                    IsDeleted = table.Column<bool>(type: "boolean", nullable: false, defaultValue: false),
                    TenantId = table.Column<Guid>(type: "uuid", nullable: true),
                    WorkflowModelId = table.Column<string>(type: "text", nullable: false),
                    WorkflowModelName = table.Column<string>(type: "text", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WorkflowModelAssocNode", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WorkflowModelAssocNode_WorkflowNode_WorkflowNodeId",
                        column: x => x.WorkflowNodeId,
                        principalTable: "WorkflowNode",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowModelAssocNode_WorkflowNodeId",
                table: "WorkflowModelAssocNode",
                column: "WorkflowNodeId");

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowModelAssocType_WorkflowModeId",
                table: "WorkflowModelAssocType",
                column: "WorkflowModeId",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowModelAssocType_WorkflowTypeId",
                table: "WorkflowModelAssocType",
                column: "WorkflowTypeId");

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowNode_WorkflowTypeId",
                table: "WorkflowNode",
                column: "WorkflowTypeId");

            migrationBuilder.CreateIndex(
                name: "IX_WorkflowType_ParentId",
                table: "WorkflowType",
                column: "ParentId");
        }
    }
}
